<?php
require_once 'config21.php';
if (!isset($_SESSION['username'])) {
    header('Location: login21.php');
    exit();
}

if (isset($_GET['id'])) {
    $department_id = $_GET['id'];
    $sql = "SELECT * FROM departments WHERE department_id = $department_id";
    $reqult = $conn->query($sql);
    if ($reqult->num_rows == 1) {
        $row = $reqult->fetch_assoc();
    } else {
        echo "未找到该系部信息";
        exit;
    }
} else {
    echo "缺少系部 ID";
    exit;
}

// 修改系部
if ($_SERVER["REQUEST_METHOD"] === "POST" && isset($_POST['edit_department'])) {
    $department_name = $_POST['department_name'];
    $sql = "UPDATE departments SET department_name = '$department_name' WHERE department_id = $department_id";
    if ($conn->query($sql)) {
        header('Location: department_management21.php');
        exit;
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
}
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>修改系部</title>
    <link rel="stylesheet" href="style.css">
</head>
<body>
    <h2>修改系部</h2>
    <form action="<?php echo $_SERVER['PHP_SELF'] . "?id=$department_id" ?>" method="post">
        <label for="department_name">系部名称:</label>
        <input type="text" name="department_name" value="<?php echo $row['department_name']; ?>" required><br>
        <input type="submit" name="edit_department" value="修改">
    </form>
    <a href="department_management21.php">返回系部管理页面</a>
</body>
</html>